package com.xiaotu.spider.project.pageprocessor.nettv;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.BaiduNewsModel;
import com.xiaotu.common.model.NetTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

@Component("BaiduNewsPageProcessor")
public class BaiduNewsPageProcessor extends AUrlPageProcessor
{

    private static final String BAIDU_NEWS_PLAY_NAME_PROUD_URL ="%E5%88%AB%E9%82%A3%E4%B9%88%E9%AA%84%E5%82%B2"; //别那么骄傲
    private static final String BAIDU_NEWS_PLAY_NAME_LOVE_URL ="%E8%BD%AC%E8%BA%AB%E8%AF%B4%E7%88%B1%E4%BD%A0"; //转身说爱你
    private static final String BAIDU_NEWS_PLAY_NAME_FIGHT_URL ="%E6%B1%82%E5%A9%9A%E5%A4%A7%E4%BD%9C%E6%88%98"; //求婚大作战
    private static final String[] START_URL = new String[]{
    		"http://news.baidu.com/ns?"
            + "word=%E5%88%AB%E9%82%A3%E4%B9%88%E9%AA%84%E5%82%B2&" //别那么骄傲 
            + "bs=%E5%88%AB%E9%82%A3%E4%B9%88%E9%AA%84%E5%82%B2&"
            + "sr=0&cl=2&rn=20&tn=news&ct=0&clk=sortbytime"
            ,
    		"http://news.baidu.com/ns?"
                    + "word=%E8%BD%AC%E8%BA%AB%E8%AF%B4%E7%88%B1%E4%BD%A0" //转身说爱你   
                    + "&bs=%E8%BD%AC%E8%BA%AB%E8%AF%B4%E7%88%B1%E4%BD%A0"
                    + "&sr=0&cl=2&rn=20&tn=news&ct=0&clk=sortbytime"
                    ,
                    "http://news.baidu.com/ns?"
                            + "word=%E6%B1%82%E5%A9%9A%E5%A4%A7%E4%BD%9C%E6%88%98" //求婚大作战 
                            + "&bs=%E6%B1%82%E5%A9%9A%E5%A4%A7%E4%BD%9C%E6%88%98"
                            + "&sr=0&cl=2&rn=20&tn=news&ct=0&clk=sortbytime"
                            };
    																							
    @Override
    public void process(Page page){
    	//用于存放当前页的数据model实体集合
    	List baiduNewsModelList=null;
    	if(page.getUrl().toString().contains(BAIDU_NEWS_PLAY_NAME_PROUD_URL)){
    		//1.别那么骄傲
    		baiduNewsModelList=getBaiduNewsModelList(page,"别那么骄傲");
    	}else if(page.getUrl().toString().contains(BAIDU_NEWS_PLAY_NAME_LOVE_URL)){
    		//2.转身说爱你
    		baiduNewsModelList=getBaiduNewsModelList(page,"转身说爱你");
    	}else if(page.getUrl().toString().contains(BAIDU_NEWS_PLAY_NAME_FIGHT_URL)){
    		//3.求婚大作战
    		baiduNewsModelList=getBaiduNewsModelList(page,"求婚大作战");
    	}
    	//导出csv文件
    	page.putField(BaiduNewsModel.class.getName() + "List", baiduNewsModelList);
    }
    
    public List getBaiduNewsModelList(Page page,String baiduNewsPlayName){
    	List<String> aFlagList = page.getHtml().xpath("//p[@id='page']/a").all();//当前页的所有a标签
    	//获取最后一个a标签
    	if(aFlagList!=null && aFlagList.size()!=0){
    	String aFlagStr=aFlagList.get(aFlagList.size()-1);
    	Html aFlagObject=new Html(aFlagStr);//把每个a标签转化为html对象
    	if(aFlagObject.xpath("//a/text()").get().contains("下一页>")){
    		String nextPageUrl=aFlagObject.xpath("//a/@href").get();
    		//添加下一页
    		PageUtils.addTargetRequest(page, nextPageUrl, page.getRequest().getExtras());
    		}
    	}else{
    		//重新抓取当前页
    		PageUtils.addTargetRequest(page, page.getUrl().toString(), page.getRequest().getExtras());
    	}
    	List<String> divList = page.getHtml().xpath("//div[@class='result']").all();
    	List<BaiduNewsModel> list = new ArrayList<>();
    	for (String div : divList) {//遍历当前页每个div对象开始
    		
			Html divObject=new Html(div);//把每个div转化为html对象
			//百度新闻剧名
			//百度新闻剧标题
			String baiduNewsPlayTitle=HtmlUtil.delHTMLTag(divObject.xpath("//div[@class='result']/h3[@class='c-title']/a").get());
			//日期和来源数组
			String baiduNewsPlayDate="";
			//百度新闻剧来源
			String baiduNewsPlayFrom="";
			String[] arr = divObject.xpath("//p[@class='c-author']/html()").get().split("&nbsp;&nbsp;");
			for (int i = 0; i < arr.length; i++) {
				//百度新闻剧日期
				baiduNewsPlayDate=arr[arr.length-1];
				//百度新闻剧来源
				baiduNewsPlayFrom=arr[0];
			}
			//百度新闻剧内容
			String baiduNewsPlayContent =null;
			String descImg = divObject.xpath("//div[@class='c-span18 c-span-last']").get();
			if(StringUtils.isNotEmpty(descImg)){
				baiduNewsPlayContent =HtmlUtil.delHTMLTag(descImg.substring(descImg.indexOf("</p>"))).replace("《  ", "《").replace("&gt;&gt;", ">>");
			}else{
				String desc = divObject.xpath("//div[@class='c-summary c-row ']").get();
				baiduNewsPlayContent =HtmlUtil.delHTMLTag(desc.substring(desc.indexOf("</p>"))).replace("《  ", "《").replace("&gt;&gt;", ">>");
			}
			list.add(new BaiduNewsModel(Constants.TargetType.OTHER_TYPE_ID,null, baiduNewsPlayName,  baiduNewsPlayTitle,  baiduNewsPlayDate,
					 baiduNewsPlayFrom,  baiduNewsPlayContent));
		}//遍历当前页每个div对象结束
		return list;
    }
    
    
    @Override
    protected String[] getStartRequestUrls()
    {
        return START_URL;
    }

    @Override
    protected String getSpiderType()
    {
        return "BAIDU_NetTV";
    }

    @Override
    public String getPageDataType(Page page)
    {
    	return Constants.PageDataType.TV_RECORD;
    }

}
